/*
	@file : perf/PerfManager.h
	@author : Wolfhead
*/
#ifndef _LOG_PERF_MANAGER_H_
#define _LOG_PERF_MANAGER_H_

#include <boost/thread.hpp>
#include <boost/noncopyable.hpp>
#include "PerfObject.h"

namespace logger
{
namespace perf
{

template<typename IdentifierType>
class PerfManager : boost::noncopyable
{
public:
	PerfObject& get_perf_object(IdentifierType id)
	{
		{
	        boost::shared_lock<boost::shared_mutex> shared_lock(m_mutex);
	        typename std::map<IdentifierType, PerfObject>::iterator it =
	            m_perf_object.find(id);
	        if (it != m_perf_object.end())
	        {
	            return *it;
	        }
	    }

	    {
	    	boost::unique_lock<boost::shared_mutex> unique_lock(m_mutex);
	    	return m_perf_object[id];
	    }
	}

	std::string flush(IdentifierType id)
	{
		PerfObject& object = get_perf_object(id);
		std::string perf = object.get_perf();

		{
	    	boost::unique_lock<boost::shared_mutex> unique_lock(m_mutex);
	    	m_perf_object.erase(id);
	    }	

	    return perf;
	}

private:

	boost::shared_mutex m_mutex;

	std::map<IdentifierType, PerfObject> m_perf_object;
};

}//namespace perf
}//namespace logger

#endif // #ifndef _LOG_PERF_MANAGER_H_
